/***************************************************************************
 *   Copyright (C) 2009 by Marian Skvarek   *
 *   skvarek.marian@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A TrackICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "CScoreView.h"
#include "CScore.h"
#include "CPart.h"
#include "CPartView.h"
#include "CGlobals.h"
#include "CUtils.h"

#include <QtGui>

using namespace easyscore::utils;
using namespace easyscore::globals;

namespace easyscore
{
	namespace core
	{

		CScoreView::CScoreView ( CScore * data )
		{
			mData = data;
			mInitialized = false;
		}

		void CScoreView::initialize()
		{
			//set initial CScoreView dimensions
                        setSceneRect ( QRect ( 0, 0, 1000, 1000 ) );
                        setBackgroundBrush(Qt::white);
			
			//initialize title
			mTitle = new QGraphicsTextItem ( mData->getSongname() + " ( " + mData->getAuthorName() + " )" );
			mTitle->setFont(QFont("Times", 255));
			addItem ( mTitle );
			mTitle->setPos ( 0, 0 );
			
			update();

			mInitialized = true;
		}

		void CScoreView::clear()
		{

			if ( !mInitialized )
				return;

			mInitialized = false;

			delete mTitle;
			mTitle = 0;
		}

		CScoreView::~CScoreView()
		{
			clear();
		}

		void CScoreView::update()
		{
			//iterate over Tracks to create & initializae their GUI			
			int currentTop = CGlobals::getScoreTopPadding();
			foreach ( CPart *part, mData->getParts() )
			{
				if(!part->hasView())
				{
					part->createView();
					CPartView *partView = part->getPartView();
					partView->setScoreView ( this );
					partView->initialize();
					
					addItem ( partView );						
				}
				
				part->getPartView()->setPos ( 0,currentTop );
				currentTop += part->getPartView()->rect().height() + CGlobals::getPartTopMargin();
			}
			
			refreshLayout();
		}

                void CScoreView::setWidth(int width)
                {

                    this->setSceneRect(0,0,width,height());

//                    int maxPartWidth = 0;
//                    foreach ( CPart *part, mData->getParts() )
//                    {
//                            if(part->hasView())
//                            {
//                                    maxPartWidth = CUtils::max ( maxPartWidth, part->getPartView()->rect().width() );
//                            }
//                    }

//                    foreach ( CPart *part, mData->getParts() )
//                    {
//                            if(part->hasView())
//                            {
//                                CPartView * partView = part->getPartView();
//                                    partView->setPos( (maxPartWidth - partView->rect().width() )/2,partView->pos().y() );
//                            }
//                    }
                }
		
		void CScoreView::refreshLayout()
		{
			int maxPartWidth = 0;
			int totalHeight = CGlobals::getScoreTopPadding();
			foreach ( CPart *part, mData->getParts() )
			{
				if(part->hasView())
				{
					maxPartWidth = CUtils::max ( maxPartWidth, part->getPartView()->rect().width() );
					totalHeight += part->getPartView()->rect().height() + CGlobals::getPartTopMargin();
				}	
			}
			
			totalHeight += CGlobals::getScoreBottomPadding();
			setSceneRect(0, 0, maxPartWidth, totalHeight);
		}



	}//namespace core
}//namespace easyscore



